#!/usr/bin/python
#***************************************************************************
# Copyright notice for this file:
#  Copyright (C) 2012 TheAxeMan
#  _______ _                               _    _
# |__   __| |     ___     /\          ___ | \  / |
#    | |  | |__  / _ \   /  \  __  _ / _ \|  \/  | __ _ _ __
#    | |  | '_ \|  __/  / /\ \ \ \/ |  __/| .  . |/ _` | '_ \
#    | |  | | | | (__/\/ ____ \ )  (| (__/| |\/| | (_| | | | |
#    |_|  |_| |_|\____/_/    \_._/\_.\____|_|  |_|\__,_|_| |_|
#
# Final Fantasy TAS scripts by TheAxeMan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#***************************************************************************

import sys
import re
import string

#need this for area name translation
from gamedata import GameData


"""
Class for segments of a run. Although simulator requires ids, the area could
be a string initially. The isPowerCycle attribute labels power cycles.
"""
class RunSegment:
  def __init__(self, area_id=0, steps=0, note=""):
    self.area_id = area_id
    self.steps = steps
    self.note = note
    self.isPowerCycle = False
  def setPowerCycle(self):
    self.area_id = -1
    self.steps = -1
    self.isPowerCycle = True

"""
This is basically just a list of RunSegments representing a run.
"""
class RunSegments:
  def __init__(self):
    self.run = []
  def clear(self):
    self.run = []
  def copy(self):
    run_copy = RunSegments()
    run_copy.run = self.run[:]
    return run_copy

  def addSegmentByData(self, area, steps):
    """helps build a list of run segments"""
    self.run.append(RunSegment(area, steps))
  def addSegment(self, segment):
    self.run.append(segment)
  def addSegments(self, run_segs):
    """
    Adds segments to end of list. The argument will have
    an empty list on finishing. Does not update option list!
    """
    while(len(run_segs.run) > 0):
      self.run.append(run_segs.run.pop(0))
    
  def addPowerCycle(self):
    segment = RunSegment()
    segment.setPowerCycle()
    self.addSegment(segment)
  def addNote(self, note):
    self.addSegment(RunSegment(0,0,note))

  def totalSteps(self):
    total = 0
    for segment in self.run:
      total += segment.steps
    return total

  def printRun(self):
    """prints out details on a list of run segments"""
    for segment in self.run:
      if segment.isPowerCycle:
        print "power cycle"
      elif segment.steps <= 0 and segment.note != "":
        print "***", segment.note, "***"
      elif segment.isPowerCycle:
        print "power cycle"
      else:
        print segment.steps, "in area", segment.area_id

  def translate(self, gdata):
    """Translates a run from named areas to area ids"""
    for segment in self.run:
      if segment.isPowerCycle or segment.steps <= 0:
        continue
      segment.area_id = gdata.translateAreaName(segment.area_id)

  def loadFromFile(self, filename):
    """load a run from a file. Format is "stepcount area_name" with comments using #"""
    self.clear()
    runfile = open(filename)
    for line in runfile:
      #strip comments
      line = re.sub("#.*", "", line)
      line = line.strip()
      #check to see if it's a note, otherwise parse as a normal segment
      match = re.search("^\s*note\s+", line)
      if match:
        note = line[match.end():]
        self.addSegment(RunSegment(0, 0, note))
      match = re.search("^\s*\d+", line)
      if not match: continue
      steps = int(match.group())
      area_name = line[match.end():]
      area_name = area_name.strip()
      self.addSegmentByData(area_name, steps)
    runfile.close()
      

def main():
  run = RunSegments()
  run.loadFromFile("wmstepList.txt")

  run.printRun()

  return

  gdata = GameData()
  gdata.loadData()

  run.translate(gdata)
  run.printRun()

  return

if __name__ == "__main__":
  main()


